package com.yyaccp.jdbc.demo;

import com.yyaccp.jdbc.core.JdbcTemplate;
import com.yyaccp.jdbc.core.PageResult;
import com.yyaccp.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PetDaoExample {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate();

    public int delete(Integer id) {
        return jdbcTemplate.executeUpdate("delete from pet where id = ?", new Object[]{id});
    }

    public int saveOrUpdate(Pet pet) {
        int count = 0;
        if(pet.getId() != null) {
            count = jdbcTemplate.executeUpdate("update pet set name=?, birthday=? where id=?",
                    new Object[] {pet.getName(), pet.getBirthday(), pet.getId()});
        } else {
            count = jdbcTemplate.executeUpdate("insert into pet(name, birthday) values(?,?)",
                    new Object[] {pet.getName(), pet.getBirthday()});
        }
        return count;
    }

    public Pet findById(Integer id) {
        return jdbcTemplate.queryForObject("select * from pet where id=?", new Object[]{id}, new PetRowMapper());
    }

    public List<Pet> findAll() {
        String sql = "select * from pet";
        return jdbcTemplate.queryForList(sql, null, new PetRowMapper());
    }

    public List<Pet> queryForList(Pet pet) {
        String sql = "select * from pet where 1=1";
        List<Object> args = new ArrayList<>();
        if (isNotEmpty(pet.getName())) {
            sql += " and name like ?";
            args.add("%" + pet.getName() + "%");
        }
        if(pet.getBirthday() != null) {
            sql += " and date(birthday) = date(?)";
            args.add(pet.getBirthday());
        }
        return jdbcTemplate.queryForList(sql, args.toArray(), new PetRowMapper());
    }

    public PageResult<Pet> queryForPage(Pet pet, int pageNo, int pageSize) {
        String sql = "select * from pet where 1=1";
        List<Object> args = new ArrayList<>();
        if (isNotEmpty(pet.getName())) {
            sql += " and name like ?";
            args.add("%" + pet.getName() + "%");
        }
        if(pet.getBirthday() != null) {
            sql += " and date(birthday) = date(?)";
            args.add(pet.getBirthday());
        }
        return jdbcTemplate.queryForPage(sql, args.toArray(), new PetRowMapper(), pageNo, pageSize);
    }



    public boolean isNotEmpty(String s) {
        return s != null && s.trim().length() > 0;
    }

    class PetRowMapper implements RowMapper<Pet> {
        @Override
        public Pet mapRow(ResultSet resultSet) throws SQLException {
            return new Pet(resultSet.getInt("id"),
                    resultSet.getString("name"),
                    resultSet.getDate("birthday"));
        }
    }
}

class Pet {
    private Integer id;
    private String name;
    private Date birthday;

    public Pet() {
    }

    public Pet(Integer id, String name, Date birthday) {
        this.id = id;
        this.name = name;
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "Pet{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", birthday=" + birthday +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}